﻿package com.wandian.view.mediator.shop
{
	import com.wandian.controller.AccountCommand;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.SwfDataProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.model.vo.RoomVo;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZLlistScrollPanel;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.util.GolbalTimer;
	import com.wandian.util.RadioManage;
	import com.wandian.view.components.ui.AddBtn;
	import com.wandian.view.components.ui.GiveGiftPlayer;
	import com.wandian.view.components.ui.MaskTipPanel;
	import com.wandian.view.components.ui.PropGiftInfo;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

    public class ShopGiftMediator extends Mediator
    {
        private var _global:GameData;
        private var _tabBar:CLZTabBar;
        private var _onPlayerVo:PlayerVo;
        private var _arrPlayer:Array;
        private var _inRoomData:InRoomPlayerDataProxy;
        private var _radioManage:RadioManage;
        private var _arrShowProp:Array;
        private var _recordPropNum:int = -1;
        private const BUYIN_MYSELF_ID:int = 1;
        private const BUYIN_ALL_SIT_PLAYER:int = 2;
        public static const NAME:String = "ShopGiftMediator";

        public function ShopGiftMediator(param1:MovieClip, param2:PlayerVo)
        {
            this._global = GameData.getInstance();
            this._arrShowProp = [];
            this._onPlayerVo = param2;
            super(NAME, param1);
            this._inRoomData = facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
            return;
        }

        override public function onRegister() : void
        {
            this.init();
            this.initPlayer();
            return;
        }

        private function init() : void
        {
            var _loc_1:Array = [[this.mcGift.giftBtn11, GameData.tip["GIFT_1"]], [this.mcGift.giftBtn12, GameData.tip["GIFT_2"]], [this.mcGift.giftBtn13, GameData.tip["GIFT_3"]], [this.mcGift.giftBtn14, GameData.tip["GIFT_4"]]];
            this._tabBar = new CLZTabBar(_loc_1);
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_1[_loc_2][0].addEventListener(Event.CHANGE, this.changeTabBarHandler);
                _loc_2 = _loc_2 + 1;
            }
            this._tabBar.setClickIndex(this.mcGift.giftBtn11);
            return;
        }

        private function initPlayer() : void
        {
            var arrGiftPlayer:Array = [this.mcGift.player1, this.mcGift.player2, this.mcGift.player3, this.mcGift.player4, this.mcGift.player5, this.mcGift.player6, this.mcGift.player7, this.mcGift.player8, this.mcGift.player9];
			var index:int = 0;
			this._arrPlayer = new Array(9);
			/*var arrSitAll:* = this._inRoomData.sitAllPlayer;
            var _loc_3:Array = [];
            while (index < 9)
            {
                
                if (arrSitAll[index]!=null&&arrSitAll[index].userid > 0)
                {
                    _loc_3.unshift(arrSitAll[index]);
                }
                else
                {
                    _loc_3.push(arrSitAll[index]);
                }
                index = index + 1;
            }*/
            index = 0;
			var playerVo:PlayerVo;
            while (index < arrGiftPlayer.length)
            {
				playerVo=this._inRoomData.getSitPlayerBySeatId(index+1);
                this._arrPlayer[index] = new GiveGiftPlayer(arrGiftPlayer[index],playerVo  );
                this._arrPlayer[index].addEventListener(Event.CHANGE, this.changeDownPlayerHandler);
                if (this._onPlayerVo != null)
                {
					if (playerVo.userid == this._onPlayerVo.userid)
					{
						this._arrPlayer[index].selectPlayer = true;
					}
                }
               
                index = index + 1;
            }
            this.mcGift.radio1_txt.text = GameData.tip["BUYIN_MYSELF"];
            this.mcGift.radio2_txt.text = GameData.tip["BUYIN_PLAYER"];
            this._radioManage = new RadioManage([[this.BUYIN_MYSELF_ID, this.mcGift.radio1_mc], [this.BUYIN_ALL_SIT_PLAYER, this.mcGift.radio2_mc]], true);
            this._radioManage.addEventListener(Event.CHANGE, this.changeRadioHandler);
            CLZButton.setButton(this.mcGift.okBuyInGift_mc, GameData.replaceStrTipHtml(GameData.tip["OK_BUYIN_GIFT"]));
            this.mcGift.okBuyInGift_mc.addEventListener(MouseEvent.CLICK, this.onBuyInGiftHandler);
            return;
        }

        private function changeRadioHandler(event:Event) : void
        {
            switch(this._radioManage._radioId)
            {
                case this.BUYIN_MYSELF_ID:
                {
                    this.selectedIsme();
                    break;
                }
                case this.BUYIN_ALL_SIT_PLAYER:
                {
                    this.selectedAllPlayer(true);
                    break;
                }
                case 0:
                {
                    this.selectedAllPlayer(false);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.contSumCostHandler();
            return;
        }

        private function changeDownPlayerHandler(event:Event) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            while (_loc_4 < this._arrPlayer.length)
            {
                
                if (this._arrPlayer[_loc_4].playerVo.userid > 0)
                {
                    _loc_2 = _loc_2 + 1;
                    if (this._arrPlayer[_loc_4].selected)
                    {
                        _loc_3 = _loc_3 + 1;
                    }
                }
                _loc_4 = _loc_4 + 1;
            }
            if (_loc_2 == _loc_3)
            {
                if (this._radioManage._radioId != this.BUYIN_ALL_SIT_PLAYER)
                {
                    this._radioManage.setRadioSelect(this.BUYIN_ALL_SIT_PLAYER, true);
                }
            }
            else if (this._radioManage._radioId == this.BUYIN_ALL_SIT_PLAYER)
            {
                this._radioManage.setRadioSelect(0, true);
            }
            this.contSumCostHandler();
            return;
        }

        private function selectedIsme() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this._arrPlayer.length)
            {
                
                if (this._arrPlayer[_loc_1].playerVo.userid == this._global.userInfo.userVo.userid)
                {
                    this._arrPlayer[_loc_1].selectPlayer = true;
                }
                else
                {
                    this._arrPlayer[_loc_1].selectPlayer = false;
                }
                _loc_1 = _loc_1 + 1;
            }
            return;
        }

        private function selectedAllPlayer(param1:Boolean) : void
        {
            var _loc_2:int = 0;
            while (_loc_2 < this._arrPlayer.length)
            {
                
                if (this._arrPlayer[_loc_2].playerVo.userid > 0)
                {
                    if (this._onPlayerVo != null)
                    {
                    }
                    if (this._arrPlayer[_loc_2].playerVo.userid == this._onPlayerVo.userid)
                    {
                    }
                    if (param1 == false)
                    {
                        this._arrPlayer[_loc_2].selectPlayer = true;
                    }
                    else
                    {
                        this._arrPlayer[_loc_2].selectPlayer = param1;
                    }
                }
                _loc_2 = _loc_2 + 1;
            }
            return;
        }

        private function changeTabBarHandler(event:Event) : void
        {
            GameData.removeNumChildre(this.mcGift.conten_mc);
            var _loc_2:* = String(event.target.name);
            var _loc_3:* = int(_loc_2.substr(_loc_2.length - 2));
            this.sendNotification(CLZNoticeName.SHOP_GIFT_LIST_REQ, _loc_3);
            return;
        }

        private function contSumCostHandler() : void
        {
            if (this._recordPropNum == -1)
            {
                return;
            }
            var _loc_1:Number = 0;
            var _loc_2:int = 0;
            while (_loc_2 < this._arrPlayer.length)
            {
                
                if (this._arrPlayer[_loc_2].playerVo.userid > 0)
                {
                }
                if (this._arrPlayer[_loc_2].selected)
                {
                    _loc_1 = _loc_1 + this._arrShowProp[this._recordPropNum].propVo.price * (this._arrShowProp[this._recordPropNum].propVo.discount / 100);
                }
                _loc_2 = _loc_2 + 1;
            }
            this.mcGift.okBuyInGift_mc.costSum_txt.htmlText = GameData.tip["SUMCOSTGOLD"] + "\n" + int(_loc_1) + GameData.tip["GOLD"] + "\n<font size=\'22\' color=\'#ffff00\'><b>" + GameData.tip["OK_BUYIN_GIFT"] + "</b></font>";
            return;
        }

        private function onBuyInGiftHandler(event:MouseEvent) : void
        {
            if (this._global.seatId == 0)
            {
                this.mcGift.okBuyInGift_mc.costSum_txt.text = GameData.tip["BUYIN_GIFT_ERR_SIT_1"];
                return;
            }
            if (this._recordPropNum == -1)
            {
                this.mcGift.okBuyInGift_mc.costSum_txt.text = GameData.tip["BUYIN_GIFT_ERR_SIT_2"];
                return;
            }
            var _loc_2:* = new Object();
            _loc_2.arrMid = [];
            var _loc_3:int = 0;
            while (_loc_3 < this._arrPlayer.length)
            {
                
                if (this._arrPlayer[_loc_3].playerVo.userid > 0)
                {
                }
                if (this._arrPlayer[_loc_3].selected)
                {
                    _loc_2.arrMid.push(this._arrPlayer[_loc_3].playerVo.userid);
                }
                _loc_3 = _loc_3 + 1;
            }
            if (_loc_2.arrMid.lenght == 0)
            {
                this.mcGift.okBuyInGift_mc.costSum_txt.text = GameData.tip["BUYIN_GIFT_ERR_SIT_3"];
                return;
            }
            var _loc_4:* = this._arrShowProp[this._recordPropNum].propVo;
            _loc_2.type = _loc_4.pcate;
            _loc_2.giftId = _loc_4.pframe;
            _loc_2.num = 1;
            this.sendNotification(CLZNoticeName.GIVE_GIFT_REQ, _loc_2);
            this.sendNotification(CLZNoticeName.HIDE_SHOPING);
            return;
        }

        private function showGiftList(arrShop:Array) : void
        {       
			if (arrShop == null||arrShop.length == 0)
			{
				return;
			} 
			GameData.removeNumChildre(this.mcGift.conten_mc);               
			var _mcGift:MovieClip = null;
            var mcListScroll:* = new CLZLlistScrollPanel(6, 102, 131, 20, 20, 288, 725);
            this.mcGift.conten_mc.addChild(mcListScroll);
            mcListScroll.x = 5;
            mcListScroll.y = 5;
            var total:* = arrShop.length;
            var index:int = 0;
            while (index < total)
            {
                
                _mcGift = GameData.getMovieClip("GiftMc");
                mcListScroll.setMc(_mcGift, index, total);
                this._arrShowProp[index] = new PropGiftInfo(_mcGift, arrShop[index], index);
                this._arrShowProp[index].addEventListener(Event.CHANGE, this.changePropHandler);
                index = index + 1;
            }
            if (this._global.seatId == 0)
            {
                this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip:GameData.tip["BUYIN_GIFT_ERR_SIT"]});
                this.mcGift.okBuyInGift_mc.costSum_txt.text = GameData.tip["BUYIN_GIFT_ERR_SIT_1"];
            }
            return;
        }

        private function changePropHandler(event:Event) : void
        {
            if (this._recordPropNum != event.target.num)
            {
            }
            if (this._recordPropNum != -1)
            {
                this._arrShowProp[this._recordPropNum].recoveryState();
            }
            this._recordPropNum = event.target.num;
            this._arrShowProp[this._recordPropNum].clickThis();
            this.contSumCostHandler();
            return;
        }

        override public function listNotificationInterests() : Array
        {
            return [CLZNoticeName.SHOP_GIFT_LIST_RES];
        }

        override public function handleNotification(notification:INotification) : void
        {
            var arr:* = notification.getBody() as Array;
            switch(notification.getName())
            {
                case CLZNoticeName.SHOP_GIFT_LIST_RES:
                {
                    this.showGiftList(arr);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        private function get mcGift() : MovieClip
        {
            return viewComponent as MovieClip;
        }

    }
}
